﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace f1文件浏览
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeTreeView();
            InitializeListView();
        }

        private void InitializeTreeView()
        {
            // 添加根目录
            TreeNode rootNode = new TreeNode("用户目录");
            rootNode.Tag = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            treeView1.Nodes.Add(rootNode);
            // 显示根目录下的所有子文件夹
            PopulateTreeView(rootNode);
        }

        private void PopulateTreeView(TreeNode rootNode)
        {
            string[] directories = Directory.GetDirectories(rootNode.Tag.ToString());
            foreach (string directory in directories)
            {
                TreeNode node = new TreeNode(Path.GetFileName(directory));
                node.Tag = directory;
                rootNode.Nodes.Add(node);
            }
        }

        private void InitializeListView()
        {
            // 设置列表视图的显示模式
            listView1.View = View.Details;
            // 添加列标题
            listView1.Columns.Add("名称");
            listView1.Columns.Add("类型");
            listView1.Columns.Add("大小");
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            // 清空列表视图
            listView1.Items.Clear();
            // 获取选中的节点
            TreeNode selectedNode = e.Node;
            // 显示选中节点的子文件夹和文件
            PopulateListView(selectedNode);
        }

        private void PopulateListView(TreeNode selectedNode)
        {
            string[] directories = Directory.GetDirectories(selectedNode.Tag.ToString());
            foreach (string directory in directories)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(directory);
                ListViewItem item = new ListViewItem(dirInfo.Name);
                item.SubItems.Add("文件夹");
                item.SubItems.Add("");
                listView1.Items.Add(item);
            }

            string[] files = Directory.GetFiles(selectedNode.Tag.ToString());
            foreach (string file in files)
            {
                FileInfo fileInfo = new FileInfo(file);
                ListViewItem item = new ListViewItem(fileInfo.Name);
                item.SubItems.Add("文件");
                item.SubItems.Add(fileInfo.Length.ToString());
                listView1.Items.Add(item);
            }
        }

        private void  listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 双击打开 txt 文件
            if (listView1.SelectedItems.Count > 0)
            {
                string selectedFile = Path.Combine(treeView1.SelectedNode.Tag.ToString(), listView1.SelectedItems[0].Text);
                if (File.Exists(selectedFile) && Path.GetExtension(selectedFile).Equals(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    // 打开 txt 文件
                    System.Diagnostics.Process.Start(selectedFile);
                }
            }
        }
    }
}
